function Pager(){
	this.page_size = this.curr_page = this.total_pages = this.total_rows =0;
	this.nid = this.nName = "";//对象id
	this.url = window.location.pathname;
	this.condition =  "";
	this.model = ""; //跳转url
	this.cache_data = new Object(); //数据缓存
	this.locked = 0; //用于操作前锁住，操作失败时，查询不显示
	this.func = new Object(); //操作
	this.id  = "pager";
}
Pager.prototype = {
	Del:function(nid,nID){		
		if(!nID&&!nID&&this.locked) return;
		this.locked = 1;
		$.ajax({
			url:this.url+set_ajx_url(this.model+"&a=delete"),
			data:nid+"="+nID,
			type: 'post',
			dataType: 'json',
			context : this,
			success : function() {
				init_page();
				this.locked = 0;
			},error:function(){
				alert(this.url+set_ajx_url(this.model+"&a=delete"));
			}
		});
	},
	JumpPage:function(i) {
		if (this.locked)return;
		var p = i;
		if (p < 0 || p > this.total_pages)return;
		page = this.cache_data[p];
		if(page) {
			$('#main_tbody').html(pager.getPageTable(page["page_head"],page["page_data"]));
			$('.main_foot_wraper').html(pager.getPageState(page["page_state"]));
			return;
		}
		this.locked = 1;
		$.ajax({
			url:this.url+set_ajx_url(this.model+"&a=ajx_init&page="+p),
			data:this.condition,//如果为post，则把数据作为请求体传递
			type : 'post',
			dataType : 'json',
			context : this,
			success : function(page) {
				if(page == null) return;
				this.curr_page = p;
				this.cache_data[p] = page;
				$('#main_tbody').html(pager.getPageTable(page["page_head"],page["page_data"]));
				$('.main_foot_wraper').html(pager.getPageState(page["page_state"]));
				this.locked = 0;
			}
		});
	},
	PreventPage:function(){
		var p = this.curr_page - 1;
		this.JumpPage(p);
	},
	NextPage : function() {
		var p = this.curr_page + 1;
		this.JumpPage(p);
	},
	LastPage : function() {
		var p = this.total_pages;
		this.JumpPage(p);
	},
	FirstPage : function() {
		var p = 1;
		page = this.cache_data[p];
		this.JumpPage(p);
	},
	getPageTable:function(head, data){
	html = "";
	/*表数据*/	
		for(var i = 0; i < data.length; i++){
			html +="<tr><td>"+(i+1)+"</td>";
			$.each(head,function(id,value){
				html += "<td>"+(data[i][value] == null ? '-' : data[i][value])+"</td>";
			});
			/*操作*/
			var n_id = this.nid;
			var n_name = this.nName;
			if(!$.isEmptyObject(this.func)){				
				$.each(this.func,function(id,value){
					html += "<td>";
					html+=value.replace(/_nID_/g,data[i][n_id]).replace(/_nid_/g,n_id).replace(/_nName_/g,data[i][n_name]);					
					html += "</td>";
				});				
			}
			html += "</tr>";			
		}
		return html;
	},
	getPageState:function(ps){
		var html = "<ul class=\"main_menu\">"
			+"<li><img src=\"images/main_pages_home.gif\" width=\"16\" height=\"16\" vspace=\"5\" onclick=\""+this.id+".FirstPage()\" title=\"首页\" style=\"cursor:pointer\"/></li>"
			+"<li><img src=\"images/main_pages_prev.gif\" width=\"16\" height=\"16\" vspace=\"5\" onclick=\""+this.id+".PreventPage()\" title=\"上一页\" style=\"cursor:pointer\"/></li>"
			+"<li>第</li>"
			+"<li class=\"font\"><div align=\"center\"><input id=\"JumpPageNumber\" name=\"textfield\" type=\"text\" class=\"txt\" style=\"text-align: center;\" size=\"5\" value=\""+Number(ps['curr_page'])+"\"/></div></li>"
			+"<li>页</li>"
			+"<li><img src=\"images/main_pages_ico2.gif\" width=\"20\" height=\"20\" vspace=\"3\" style=\"cursor:pointer\" onclick=\""+this.id+".JumpPage($('#JumpPageNumber').val())\"/></li>"
			+"<li><img src=\"images/main_pages_next.gif\" width=\"16\" height=\"16\" vspace=\"5\" onclick=\""+this.id+".NextPage()\" title=\"下一页\" style=\"cursor:pointer\"/></li>"
			+"<li><img src=\"images/main_pages_end.gif\" width=\"16\" height=\"16\" vspace=\"5\" onclick=\""+this.id+".LastPage()\" title=\"最后一页\"style=\"cursor:pointer\"/></li>"
			+"<li><img src=\"images/main_pages_line.gif\" width=\"16\" height=\"16\" vspace=\"5\" /></li>"
			+"<li>共"+Number(ps['total_pages'])+"页</li>"
			+"<li><img src=\"images/main_pages_line.gif\" width=\"16\" height=\"16\" vspace=\"5\" /></li>"
			+"<li>共"+Number(ps["total_rows"])+"条</li>"
			+"</ul>";
		this.curr_page= Number(ps["curr_page"]);
		this.page_size=Number(ps["page_size"]);
		this.total_pages=Number(ps["total_pages"]);
		this.total_rows=Number(ps["total_rows"]);
		return html;		
	},
	setEdit:function(){
		this.func["update_btn"]="<a href=\""+this.model+"&a=edit&_nid_=_nID_&ron_hash="+ron_hash+"\"><img src=\"images/main_qy.gif\" width=\"16\" height=\"16\" /></a>";
	},
	setDel:function(){
		this.func["del_btn"]="<a href=\"javascript:void(0)\" onClick=\"confirm('确定删除吗？')? "+this.id+".Del('_nid_',_nID_) :''\" ><img src=\"images/main_sc.gif\" width=\"16\" height=\"16\" /></a>";
	},
	setselect:function(){
		this.func["sel_btn"] = "<a href=\"javascript:void(0)\" onClick=\"Open_window.selMID('_nName_','_nID_')\"><img src=\"images/plugin_debug.png\" width=\"16\" height=\"16\" title=\"选定\"/></a>";
	},
	search:function(){
		var _condition="";
		$('.search_Within :input').each(function(k,v){
			if(!v.id||$.trim(v.value)=="")return true;
			_condition += v.id +"="+ $.trim(v.value) + "&";
		});
		this.locked=1;
		this.condition=_condition;
		$.ajax({
			url:this.url+set_ajx_url(this.model+"&a=ajx_init"),
			data:this.condition,
			type : 'post',
			dataType : 'json',
			context : this,
			success:function(page){
				if(page == null) return;
				this.cache_data=new Object();
				this.cache_data[0] = page;
				$('#main_tbody').html(pager.getPageTable(page["page_head"],page["page_data"]));
				$('.main_foot_wraper').html(pager.getPageState(page["page_state"]));
			
				this.locked = 0;
			},error:function(page){
				$('#main_tbody').html(print_r(page));
			}
		});
	}
};
